package daos;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import model.entities.AbstractEntity;

public class AbstractDAO<E extends AbstractEntity> {
	
	EntityManager manager;
	
	public AbstractDAO(EntityManager manager) {
		this.manager = manager;
	}

	public void salvar(E entity){
		manager.persist(entity);
	}
	
	public void remover(E entity){
		manager.remove(entity);
	}
	
	public Class<E> entityClass(){
		return null;
	}
	
	public E buscarPorId(Long id) {
		return manager.getReference(entityClass(), id);
	}
	
	
	public List<E> listarTodos(){
		Query query = manager.createNamedQuery("Select e from "
				      +entityClass().getSimpleName()+ " e ");
		return query.getResultList();
	}
	
	//adicionado de CC
		public void atualizar(E entity){
			E e = buscarPorId(entity.getId());
			manager.persist(e);
		}

	
}
